package com.gyt.manager.common.component;

import com.alibaba.fastjson.JSON;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;

/**
 * description.
 *
 * @author gyt create on 2021/6/18 16:52
 */
@Component
public class CosComponent {

    private long appId = 1305389643;
    private String secretId = "AKIDGL9hzziANJzOWNxGI4ZmiRyJOEpAF3bs";
    private String secretKey = "3yhAMH4g8YqyvOn5I5i7NacnocCXcHl0";
    private COSClient cosClient;
    private String bucketName = "testbucket";

    @PostConstruct
    public void init(){
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);
        cosClient = new COSClient(cred, clientConfig);

    }

    public boolean createBucket(){
        String bucket = String.format("%s-%s", bucketName, appId);
        CreateBucketRequest request = new CreateBucketRequest(bucket);
        request.setCannedAcl(CannedAccessControlList.Private);
        Bucket bucketResult = null;
        try {
            bucketResult = cosClient.createBucket(request);
        } catch (CosClientException e) {
            //BucketAlreadyOwnedByYou
            System.out.println(e.getErrorCode());
        }
        System.out.println(JSON.toJSONString(bucketResult));
        return false;
    }

    public void uploadFile(){
        try {
            String bucket = String.format("%s-%s", bucketName, appId);
            Resource resource = new ClassPathResource("/config/templates/20200505082448.xlsx");

            File file = new File("F:\\test.py");
            String name = file.getName();
            if (!file.exists() || file.length() == 0) {

            }

            String s = name.split("[.]")[0];
            System.out.println(name);
            String key = name;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, file);
            //{"dateStr":"Tue, 22 Jun 2021 10:36:42 GMT","metadata":{"contentLength":0,"deleteMarker":false,"instanceLength":0,
            // "rawMetadata":{"X-Cache":"MISS from SZ-SQUIDWEB-73","X-Cos-Hash-Crc64ecma":"11052549537962972966","Etag":"3028879ab8d5c87dc023049fa5bb5c1a",
            // "X-Cache-Lookup":"MISS from SZ-SQUIDWEB-73:8080","Content-Length":0,
            // "X-Cos-Request-Id":"NjBkMWJkM2FfNzJjYzNiMGJfNGY5MV82NjAwNGE2","Date":"Tue, 22 Jun 2021 10:36:42 GMT"},"userMetadata":{}}}
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            System.out.println(JSON.toJSONString(putObjectResult));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public COSObjectInputStream  downloadFile(){
        String bucket = String.format("%s-%s", bucketName, appId);
        String key = "test.py";
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        COSObjectInputStream inputStream = cosObject.getObjectContent();
        return inputStream;
    }
}
